<template>
  <div>
    <div id="slider" class="mui-slider">
      <div
        id="sliderSegmentedControl"
        class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted"
      >
        <div class="mui-scroll">
          <a :class="['mui-control-item',item.id === 0 ? 'mui-active' : '']"
            v-for="item in catsList" :key="item.id" @click="getPhotoListByCateId(item.id)">{{ item.title }}</a>
        </div>
      </div>
    </div>

    <ul class="photo-list">
      <router-link v-for="item in photoList" :key="item.id" :to="'/home/photoinfo/' + item.id" tag="li">
        <img v-lazy="item.img_url" />
        <div class="photo-info">
          <h1 class="photo-info-title">{{ item.title }}</h1>
          <div class="photo-info-des">{{ item.zhaiyao }}</div>
        </div>
      </router-link>
    </ul>
  </div>
</template>

<script>
import mui from "../../lib/mui/js/mui.min.js";

export default {
  // 创建时初始化类别列表
  created() {
    this.getAllCats();
    this.getPhotoListByCateId(0);
  },

  // 挂在到dom时，调用mui的方法
  // flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
  mounted() {
    mui(".mui-scroll-wrapper").scroll({
      deceleration: 0.0005
    });
  },

  // 返回当前vue中需要的数据，主要是类别列表数组
  data() {
    return {
      catsList: [],
      photoList: []
    };
  },

  methods: {
    // 获取图片分类的方法，注意“全部”这个类别需要手工添加
    getAllCats() {
      this.$http.get("api/getimgcategory").then(res => {
        if (res.body.status == 0) {
          this.catsList = res.body.message;
          this.catsList.unshift({
            title: "All",
            id: 0
          });
          // console.log("Cats list loading complete");
        } else {
          Toast("Loading error...");
        }
      });
    },

    //根据图片分类id，获取所有图片对象的列表
    getPhotoListByCateId(cateId) {
      this.$http.get("api/getimages/" + cateId).then(res => {
        if (res.body.status == 0) {
          this.photoList = res.body.message;
          // console.log("Photo list loading complete");
        } else {
          Toast("Loading error...");
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
* {
  touch-action: pan-y;
}

.photo-list {
  list-style: none;
  padding: 10px;
  margin: 0;
  padding-bottom: 0px;
  li {
    background-color: #ccc;
    text-align: center;
    margin-bottom: 10px;
    position: relative;
    box-shadow: 0 0 9px #999;

    img {
      width: 100%;
      vertical-align: middle;
    }

    img[lazy="loading"] {
      width: 40px;
      height: 300px;
      margin: auto;
    }

    .photo-info {
      max-height: 84px;
      color: white;
      text-align: left;
      bottom: 0;
      position:absolute;
      background-color: rgba(0,0,0,0.4);
      .photo-info-title {
        font-size: 14px;
      }
      .photo-info-des {
        font-size: 13px;
      }
    }
  }
}
</style>